package jdbc.dao;

import jdbc.main.User;
import untils.JDBCUtils;

import javax.print.attribute.standard.JobHoldUntil;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by guo on 2017/9/13.
 */
public class UserDao {
    public List<User> findAll() throws SQLException {
        List<User> linkedList = null;
        Statement statement=null;
        ResultSet set=null;
        Connection con= JDBCUtils.getConetion();
        User user=new User();
        try {
         linkedList=new LinkedList<>();
            String sql="select * from User";
             statement=con.createStatement();
          set=  statement.executeQuery(sql);
            while (set.next())
            {
                user.getId(set.getInt(1));
                user.getUnname(set.getString(2));
                user.getPhone(set.getString(3));
                linkedList.add(user);
            }
            return linkedList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            if(statement!=null)
            {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(set!=null)
            {
                try {
                    set.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            con.close();
        }
        return  null;
    }
    public boolean insert(User user) {
//        User user=new User();
        PreparedStatement ps = null;
        Connection con = JDBCUtils.getConetion();
        String sql = "insert into user(id,username,phone) values(?,?,?)";

        try {
            ps = con.prepareStatement(sql);
           ps.setInt(1,user.getId());
           ps.setString(2,user.getUnname());
           ps.setString(3,user.getPhone());
            int num = ps.executeUpdate();
            if (num == 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            if(ps!=null)
            {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
return true;
    }

//    public boolean Update()
//    {
//        PreparedStatement ps=null;
//        Connection con=null;
//        User user=new User();
//        String sql=
//    }
    public User findId(int id) {

      PreparedStatement ps=null;
        ResultSet set=null;
        Connection con=JDBCUtils.getConetion();
        User user=new User();
        try {
            String sql="select * from User where id=? ";
            ps=con.prepareStatement(sql);
          ps.setInt(1,id);
          set=ps.executeQuery();
            while (set.next())
            {
                user.getId(set.getInt(1));
                user.getUnname(set.getString(2));
                user.getPhone(set.getString(3));
                return user;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            if(ps!=null)
            {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(set!=null)
            {
                try {
                    set.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  null;
    }

    public boolean delete(int id) throws SQLException {
        Connection con=null;
        PreparedStatement ps=null;
        String sql="delete from user where id = ?";
        try{
            con = JDBCUtils.getConetion();

            ps= con.prepareStatement(sql);
            ps.setInt(1,id);
            int num=ps.executeUpdate();
            if(num==1)
            {
                return true;
            }
            else
                return false;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally {
        if(ps!=null)
        {
            ps.close();
        }
        }
        return true;
    }
    public boolean update(User user)
    {
        PreparedStatement ps=null;
        Connection con=null;
        String sql="update from user set usename=?,phone=? where id = ?";

            try {
                con=JDBCUtils.getConetion();
                ps=con.prepareStatement(sql);
                ps.setString(1,user.getUnname());
                ps.setString(2,user.getPhone());
                ps.setInt(3,user.getId());
                int num=ps.executeUpdate();
                if(num==1)
                {
                    return true;
                }
                else
                    return false;
            } catch (SQLException e1) {
                e1.printStackTrace();
            }

        finally {
                if (ps != null)
            {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            }
            return true;
    }
}
